﻿namespace CodeGenerator.CSharp
{
    public class Constructor: CodeUnit
    {
        public Constructor(string name, ModifierCollection modifiers) : base(name, modifiers) { }
        public Constructor(string name, ModifierCollection modifiers, string dataType)
            : base(name, modifiers) 
        { this.DataType = dataType; }

        public string Base { get; set; }
        public string This { get; set; }

        public override string ToString()
        {
            CodeBuilder Code = new CodeBuilder();

            this.AppendDocumentation(Code);
            this.AppendAttributes(Code);

            string CloseIfAbstract = this.Modifiers.IsAbstract ? ";" : string.Empty;

            string BaseContructor = string.Empty;
            if (this.Base != null)
                BaseContructor = " : base(" + this.Base + ")";
            else if(this.This != null)
                BaseContructor = " : this(" + this.This + ")";
            //string BaseString = string.IsNullOrEmpty(this.Base) ? string.Empty : " : base(" + this.Base + ")";
            //string ThisString = string.IsNullOrEmpty(this.This) ? string.Empty : " : this(" + this.This + ")";
            Code.Add("{0}{1} {2}({3}){4}{5}", this.Padding, this.Modifiers, this.Name, this.Parameters, CloseIfAbstract, BaseContructor);
            if (this.Modifiers.IsNotAbstract)
            {
                Code.Add(this.Padding + "{");
                Code.Add(this.Body);
                Code.Add(this.Padding + "}");
            }

            return Code.ToString();
        }

    }
}